package com.monitor.service.impl;

import com.monitor.entity.ErpParameters;
import com.monitor.entity.ErpTpmsTireInformation;
import com.monitor.dao.ErpParametersMapper;
import com.monitor.dao.ErpTpmsTireInformationMapper;
import com.monitor.service.ErpTpmsTireInformationService;
import com.monitor.util.Result;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.text.DecimalFormat;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 客户轮胎报警值设置表 服务实现类
 * </p>
 *
 * @author LX
 * @since 2018-10-09
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class ErpTpmsTireInformationServiceImpl extends ServiceImpl<ErpTpmsTireInformationMapper, ErpTpmsTireInformation> implements ErpTpmsTireInformationService {

	@Resource
	private ErpTpmsTireInformationMapper erpTpmsTireInformationMapper;
	
	@Resource
	private ErpParametersMapper erpParametersMapper;
	
	@Override
	public Result<List<ErpTpmsTireInformation>> selectList(int custId) {
		// TODO Auto-generated method stub.
		List<ErpTpmsTireInformation> list=erpTpmsTireInformationMapper.selectList(new EntityWrapper<ErpTpmsTireInformation>().eq("pid", custId).orderBy("id", false));
		return new Result<List<ErpTpmsTireInformation>>(list, list.size());
	}

	@Override
	public Result<Integer> addInformation(ErpTpmsTireInformation forma) {
		// TODO Auto-generated method stub
		try {
			Integer count=erpTpmsTireInformationMapper.insert(forma);
			return new Result<Integer>(count, 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<>(1, e.getMessage());
		}
	}

	@Override
	public Result<Integer> updateInformation(ErpTpmsTireInformation forma) {
		// TODO Auto-generated method stub
		try {
			Integer count=erpTpmsTireInformationMapper.updateAllColumnById(forma);
			return new Result<Integer>(count, 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<>(1, e.getMessage());
		}
	}

	@Override
	public Result<Integer> updateAll(ErpTpmsTireInformation forma) {
		// TODO Auto-generated method stub
		try {
			Integer count=0;
			List<ErpParameters> list=erpParametersMapper.selectList(new EntityWrapper<>());
			List<ErpTpmsTireInformation> tpmsList=erpTpmsTireInformationMapper.selectList(new EntityWrapper<ErpTpmsTireInformation>().eq("pid", forma.getPid()));
			for(ErpParameters para:list) {
				for(ErpTpmsTireInformation tpms:tpmsList) {
					if(tpms.getBrand().equals(para.getArand())&&tpms.getModel().equals(para.getModel())&&tpms.getPlace().equals(para.getAddress())&&tpms.getSpecifications().equals(para.getNorms())) {
						if(forma.getSpeed()!=null) {
							tpms.setSpeed(forma.getSpeed());
						}
						if(forma.getPressure()!=null&&forma.getPressure()>0) {
							if(para.getPressure()!=null&&!para.getPressure().equals("")) {
								Double pressure=Double.valueOf(para.getPressure());
								Double zhi=forma.getPressure()/100;
								zhi=zhi*pressure;
								Double shu=zhi+pressure;
								shu=getTwoDecimal(shu);
								tpms.setPressure(shu);
							}
						}
						if(forma.getPressureSmall()!=null) {
							if(para.getPressure()!=null&&!para.getPressure().equals("")) {
								Double pressure=Double.valueOf(para.getPressure());
								Double zhi=forma.getPressureSmall()/100;
								zhi=zhi*pressure;
								Double shu=pressure-zhi;
								shu=getTwoDecimal(shu);
								tpms.setPressureSmall(shu);
							}
						}
						if(forma.getTemperature()!=null) {
							tpms.setTemperature(forma.getTemperature());
						}
						if(forma.getVoltageSmall()!=null) {
							tpms.setVoltageSmall(forma.getVoltageSmall());
						}
						count=erpTpmsTireInformationMapper.updateAllColumnById(tpms);
					}
				}
			}
			
			return new Result<Integer>(count, 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<>(1, e.getMessage());
		}
	}

	@Override
	public Result<Integer> equivalent(int pid,int cid) {
		// TODO Auto-generated method stub
		try {
			List<ErpTpmsTireInformation> list=erpTpmsTireInformationMapper.selectList(new EntityWrapper<ErpTpmsTireInformation>().eq("pid", pid));
			Integer count=0;
			erpTpmsTireInformationMapper.delete(new EntityWrapper<ErpTpmsTireInformation>().eq("pid", cid));
			for(ErpTpmsTireInformation ti:list) {
				ti.setPid(cid);
				ti.setId(null);
				erpTpmsTireInformationMapper.insert(ti);
				count++;
			}
			
			return new Result<Integer>(count, 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<Integer>(1, e.getMessage());
		}
	}

	private Double getTwoDecimal(Double num) {
        DecimalFormat dFormat = new DecimalFormat("#.00");
        String yearString = dFormat.format(num);
        Double temp = Double.valueOf(yearString);
        return temp;
   }

}
